package com.qixin.activiti;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.qixin.activiti.listenter.*;
import com.qixin.activiti.service.*;
import com.qixin.modules.log.entity.SysInterfaceLog;
import com.qixin.modules.log.service.ISysInterfaceLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.delegate.DelegateExecution;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.modules.activiti.entity.ActBusiness;
import org.jeecg.modules.activiti.service.IActBusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.convert.QueryMapper;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
* @Description: 手动推送NC报销接口
* @author: CYS
* @date: 2022/3/15 9:46
*/
@Api(tags="手动推送NC报销接口")
@Slf4j
@RestController
@RequestMapping("/system/listenter/ncBxItf")
public class ManualPushNCBXItfController {

    @Autowired
    private IEaCommunicationsBXNCItfService iEaCommunicationsBXNCItfService;
    @Autowired
    private IEaEntertainBXNCItfService iEaEntertainBXNCItfService;
    @Autowired
    private IEaLoanbillBXNCItfService iEaLoanbillBXNCItfService;
    @Autowired
    private IEaPaymentBXNCItfService iEaPaymentBXNCItfService;
    @Autowired
    private IEaRepaymentbillBXNCItfService iEaRepaymentbillBXNCItfService;
    @Autowired
    private IEaTravelBXNCItfService iEaTravelBXNCItfService;
    @Autowired
    private ISysInterfaceLogService iSysInterfaceLogService;
    @Autowired
    private IActBusinessService actBusinessService;

    /***
    * @Description: 报销单据推送NC报销单据审批通过接口
    * @author: CYS
    * @date: 2022/3/15 15:12
    * @param sysInterfaceLog1:
    * @Return: org.jeecg.common.api.vo.Result
    */
    @AutoLog(value = "报销单据推送NC报销单据审批通过接口")
    @ApiOperation(value="报销单据推送NC报销单据审批通过接口", notes="报销单据推送NC报销单据审批通过接口")
    @RequestMapping(value = "/manualPushNCBXApproveItf", method = RequestMethod.POST)
    public Result manualPushNCBXApproveItf(@RequestBody SysInterfaceLog sysInterfaceLog1) {
        //结果日志
        SysInterfaceLog sysInterfaceLog2=null;

        try {
            QueryWrapper<ActBusiness> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("table_id", sysInterfaceLog1.getBusinessId());

            ActBusiness actBusiness = actBusinessService.getOne(queryWrapper);
            if(sysInterfaceLog1.getName().contains("eaCommunications")){
                sysInterfaceLog2 = iEaCommunicationsBXNCItfService.bXApprove(actBusiness, sysInterfaceLog1.getBusinessNo());
                sysInterfaceLog2.setName("手动推送NC通讯费用报销审核通过接口eaCommunications");
            }else if(sysInterfaceLog1.getName().contains("eaEntertain")){
                sysInterfaceLog2 = iEaEntertainBXNCItfService.bXApprove(actBusiness, sysInterfaceLog1.getBusinessNo());
                sysInterfaceLog2.setName("手动推送NC招待费用报销审核通过接口eaEntertain");
            }else if(sysInterfaceLog1.getName().contains("eaLoanbill")){
                sysInterfaceLog2 = iEaLoanbillBXNCItfService.bXApprove(actBusiness, sysInterfaceLog1.getBusinessNo());
                sysInterfaceLog2.setName("手动推送NC推送借款单审核通过接口eaLoanbill");
            }else if(sysInterfaceLog1.getName().contains("eaPayment")){
                sysInterfaceLog2 = iEaPaymentBXNCItfService.bXApprove(actBusiness, sysInterfaceLog1.getBusinessNo());
                sysInterfaceLog2.setName("手动推送NC资金审批单审核通过接口eaPayment");
            }else if(sysInterfaceLog1.getName().contains("eaRepaymentbill")){
                sysInterfaceLog2 = iEaRepaymentbillBXNCItfService.bXApprove(actBusiness, sysInterfaceLog1.getBusinessNo());
                sysInterfaceLog2.setName("手动推送NC还款单审核通过接口eaRepaymentbill");
            }else if(sysInterfaceLog1.getName().contains("eaTravel")){
                sysInterfaceLog2 = iEaTravelBXNCItfService.bXApprove(actBusiness, sysInterfaceLog1.getBusinessNo());
                sysInterfaceLog2.setName("手动推送NC差旅费用报销审核通过接口eaTravel");
            }else{
                throw new RuntimeException("没有对应接口名称报销单");
            }

            //错误
            if(sysInterfaceLog2.getState().equals("0")){
                iSysInterfaceLogService.saveInterfaceLog(sysInterfaceLog2);
                throw new RuntimeException();
            }

            //成功
            UpdateWrapper<SysInterfaceLog> updateWrapper=new UpdateWrapper<>();
            updateWrapper.eq("business_no",sysInterfaceLog1.getBusinessNo());
            iSysInterfaceLogService.update(new SysInterfaceLog().setDisposeState("2"),updateWrapper);

        }catch (Exception e){
            return Result.error("推送失败！("+e.getMessage()+")");
        }

        return  Result.OK();
    }

}
